package com.dhecp.project.base.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.core.controller.BaseController;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.core.layui.GridColumnsPropertyModel;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.base.domain.CompanyInfo;
import com.dhecp.project.base.service.CompanyInfoService;
import com.dhecp.project.system.domain.SysDictionaryForPageField;
import com.dhecp.project.system.service.SysDictionaryForFieldService;

/**
 * 公司信息 控制层
 * @author DH-Xxj
 * @version 2019年11月4日
 */
@Controller
@RequestMapping("/base/company")
public class CompanyInfoController extends BaseController {
    private String prefix = "base/company";
    private String moduleTable = ModuleTable.COMPANY_INFO;

    @Autowired
    private CompanyInfoService companyInfoService;

    @Autowired
    private SysDictionaryForFieldService dictionaryForFieldService;

    @Autowired
    private GridColumnsPropertyModel gridColumnsPropertyModel;

    /**
     * 打开公司信息查询页
     * @param params
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("base:company:view")
    @GetMapping()
    public ModelAndView companyInfoList(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	params.put("entity", new CompanyInfo()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	
    	Subject userSubject = ShiroUtils.getSubject();
    	Integer toolBarWidth = 0;
    	if (userSubject.isPermitted("base:company:delete")){	//当前登录用户主体是否有删除的权限
//        	params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
        	toolBarWidth = toolBarWidth + 60;
    	}
    	if (userSubject.isPermitted("base:company:edit")){ //当前登录用户主体是否有修改的权限
    		toolBarWidth = toolBarWidth + 60;
    	} 
    	params.put("toolBarWidth", toolBarWidth);
    	
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        List<SysDictionaryForPageField> dicFieldsList = new ArrayList<SysDictionaryForPageField>();
        List<Object> fieldsList = new ArrayList<Object>(); 
        List<String> fieldRequiredList = new ArrayList<String>(); 
        try{
        	dicFieldsList = dictionaryForFieldService.queryFieldAttributesList(params);
            if (dicFieldsList.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
                ajaxResult = AjaxResult.success("数据获取成功。");
            	for (SysDictionaryForPageField dicFields : dicFieldsList){
            		Map<String, Object> fieldsListMap = TypeUtils.entityToMap(dicFields);
            		fieldsList.add(TypeUtils.mapToJson(fieldsListMap));
            		
            		if (dicFields.getFieldFrontRequired() != null && dicFields.getFieldFrontRequired()){
            			fieldRequiredList.add(dicFields.getFields());
            		}
            	}
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("ajaxResult", ajaxResult); //查询结果状态
        mv.addObject("fieldRequiredList", fieldRequiredList); //必填字段
        mv.addObject("fieldsList", fieldsList); //生成控件
        mv.addObject("moduleTable", moduleTable); //moduleTable
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params)); //生成表格
        mv.setViewName(prefix + "/company");
        return mv;
    }    
    
    /**
     * 打开公司信修改页
     * @param vId 必要参数：公司编码
     * @return ModelAndView
     * @author DH-Xxj
     */
    @RequiresPermissions("base:company:edit")
    @GetMapping("/edit")
    public ModelAndView companyInfoEdit(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	params.put("entity", new CompanyInfo()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        List<SysDictionaryForPageField> dicFieldsList = new ArrayList<SysDictionaryForPageField>();
        List<Object> fieldsList = new ArrayList<Object>();
        List<Object> fieldsHiddenList = new ArrayList<Object>(); 			//隐藏的字段和属性
        List<String> fieldRequiredList = new ArrayList<String>(); 
        try{
        	dicFieldsList = dictionaryForFieldService.queryFieldAttributesList(params);
            if (dicFieldsList.isEmpty()){
                ajaxResult = AjaxResult.error("未获取到字段属性。");
            }else{
            	CompanyInfo companyInfo = companyInfoService.queryCompanyInfoByVId(params);
            	if (companyInfo == null){
            		ajaxResult = AjaxResult.error("没有查到公司[" + params.get("vId").toString() + "]的信息。");
            	}else{
                	ajaxResult = AjaxResult.success("数据获取成功。");
                	mv.addObject("companyInfo",companyInfo);
                	for (SysDictionaryForPageField dicFields : dicFieldsList){
                		Map<String, Object> fieldsListMap = TypeUtils.entityToMap(dicFields);
                		fieldsListMap.put("fieldContent", TypeUtils.entityToMap(companyInfo).get(fieldsListMap.get("fields")));
                		if ((Integer)fieldsListMap.get("fieldFrontTop") == 1){ //显示的表头
                    		fieldsList.add(TypeUtils.mapToJson(fieldsListMap));
                		}else if ((Integer)fieldsListMap.get("fieldFrontTop") == 2){ //隐藏的表头
                			fieldsHiddenList.add(TypeUtils.mapToJson(fieldsListMap));
                		}
                		if (dicFields.getFieldFrontRequired() != null && dicFields.getFieldFrontRequired()){
                			fieldRequiredList.add(dicFields.getFields());
                		}
                	}
            	}
            }
        }
        catch(Exception e){
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        mv.addObject("ajaxResult", ajaxResult); //查询结果状态
        mv.addObject("fieldRequiredList", fieldRequiredList); //必填字段
        mv.addObject("fieldsList", fieldsList); //生成控件
        mv.addObject("fieldsHiddenList", fieldsHiddenList); //隐藏的表头
        mv.setViewName(prefix + "/company_edit");
        return mv;
    }

    /**
     * 按条件查询公司信息列表
     * @param params
     * @return TableDataInfo
     * @author DH-Xxj
     */
//    @RequiresPermissions("base:company:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo companyInfoListJson(@RequestParam Map<String, Object> params){
    	params.put("moduleTable", moduleTable);
    	List<CompanyInfo> list = new ArrayList<CompanyInfo>();
    	try{
    		list = companyInfoService.queryCompanyInfoList(params);
    	}catch(Exception e){
    		return getDataTable(this.getExceptionMessage(e).toString());
    	}
    	return getDataTable(list);
    }
    
    /**
     * 打开公司选择页面
     * @param params
     * @return ModelAndView
     * @author lwl
     */
    @GetMapping("/select")
    public ModelAndView selectModule(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
    	params.put("entity", new CompanyInfo()); //生成表格gridColumns对应的实体，用于获取实体属性生成表格字段
    	params.put("toolBarWidth", 0);
    	if(params.get("pagetype") !=null && params.get("pagetype").toString().equalsIgnoreCase("checkbox"))params.put("checkBox", "yes");
        else params.put("radio", "yes");
        ModelAndView mv = this.getModelAndView();
        AjaxResult ajaxResult = new AjaxResult();
        Map<String, Object> dictionaryForFieldMap = new HashMap<String, Object>();
        
        mv.addObject("fields", TypeUtils.mapToJson(dictionaryForFieldMap));
        mv.addObject("ajaxResult", ajaxResult);
        mv.addObject("gridColumns", gridColumnsPropertyModel.queryGridColumnsPropertyModel(params));
        mv.addObject("prefix", prefix);
        mv.addObject("moduleTable", moduleTable);
        mv.setViewName("common/select");
        return mv;
    }

    /**
	 * 查询打印报表关联公司信息
	 * @param cid
	 * @return AjaxResult 
	 * @author DH-Xxj
     * @throws Exception 
	 */
    @RequestMapping("/getinfobyvid")
    @ResponseBody
    public AjaxResult getInfoByVid(@RequestParam Map<String, Object> params) throws Exception{
    	AjaxResult ajaxResult = new AjaxResult();
        CompanyInfo companyInfo = companyInfoService.queryCompanyInfoByVId(params);
        if (companyInfo == null){
            ajaxResult =  AjaxResult.error("没有符合条件的数据。");
        }else{
            ajaxResult = AjaxResult.success("查询成功。", companyInfo);
        }
        return ajaxResult;
    }
}
